﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLNhaThuoc.DTO;
using System.Data;
using System.Collections;

namespace QLNhaThuoc.DAL
{
    class PhieuXuatDA:DataProvider
    {
        private CTPhieuXuatDA ctpxDA = new CTPhieuXuatDA();

        public PhieuXuatDA()
        {
            connect();
            fillMyTable("PHIEU_XUAT");
        }

        public void insert(PhieuXuat info)
        {
            DataRow row = myTable.NewRow();
            //row["MaPX"] = info.MaPX;
            row["MaNVXuat"] = info.MaNVXuat;
            row["NgayXuat"] = info.NgayXuat;
            row["LyDo"] = info.LyDo;
            row["ThanhTien"] = info.ThanhTien;
            row["MaNVNhan"] = info.MaNVNhan;
            myTable.Rows.Add(row);

            updateMyTable();

            foreach (CTPhieu ctpx in info.ctPhieuXuat)
            {
                ctpx.Ma = int.Parse(row["MaPX"].ToString());
                ctpxDA.insert(ctpx);
            }
        }

        public void delete(PhieuXuat info)
        {
            foreach (CTPhieu ctpx in info.ctPhieuXuat)
            {
                ctpxDA.delete(ctpx);
            }

            myTable.Rows.Find(info.MaPX).Delete();

            updateMyTable();          
        }

        public void update(PhieuXuat info)
        {
            DataRow row = myTable.Rows.Find(info.MaPX);
            //row["MaPX"] = info.MaPX;
            row["MaNVXuat"] = info.MaNVXuat;
            row["NgayXuat"] = info.NgayXuat;
            row["LyDo"] = info.LyDo;
            row["ThanhTien"] = info.ThanhTien;
            row["MaNVNhan"] = info.MaNVNhan;

            updateMyTable();

            foreach (CTPhieu ctpx in info.ctPhieuXuat)
            {
                ctpxDA.update(ctpx);
            }
        }

        public bool KiemTraPK(PhieuXuat info)
        {
            DataRow row = myTable.Rows.Find(info.MaPX);
            if (row != null)
                return true;
            return false;
        }

        public PhieuXuat[] search(string expr)
        {
            string sqlString = "select * from PHIEU_XUAT where " + expr;
            ArrayList list = DongHopDuLieuTuCustomDatatable(sqlString);
            int n = list.Count;
            PhieuXuat[] kq = new PhieuXuat[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (PhieuXuat)list[i];
            }

            return kq;
        }

        public PhieuXuat[] LayListPhieuXuat()
        {
            ArrayList list = DongHopDuLieuTuDataTable();
            int n = list.Count;
            PhieuXuat[] kq = new PhieuXuat[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (PhieuXuat)list[i];
            }

            return kq;
        }

        public override object ConvertOneRowIntoDTO(DataTable table, int i)
        {
            PhieuXuat obj = new PhieuXuat();
            obj.MaPX = int.Parse(table.Rows[i]["MaPX"].ToString());
            obj.NgayXuat = DateTime.Parse(table.Rows[i]["NgayXuat"].ToString());
            obj.MaNVNhan = table.Rows[i]["MaNVNhan"].ToString();
            obj.MaNVXuat = table.Rows[i]["MaNVXuat"].ToString();
            obj.LyDo = table.Rows[i]["LyDo"].ToString();
            obj.ThanhTien = int.Parse(table.Rows[i]["ThanhTien"].ToString());
            obj.ctPhieuXuat = ctpxDA.LayListCTPhieu(obj.MaPX);

            return obj;
        }
    }
}
